package com.ashin;

import org.neo4j.driver.*;

import static org.neo4j.driver.Values.parameters;

/**
 * @author Ashin
 */
public class HelloWorldExample implements AutoCloseable {

    private final Driver driver;

    public HelloWorldExample(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }

    @Override
    public void close() throws Exception {
        driver.close();
    }

    public void printGreeting(final String message) {
        try (Session session = driver.session()) {
            String greeting = session.writeTransaction(tx -> {
                Result result = tx.run("create (a:Greeting) " +
                                "SET a.message = $message " +
                                "RETURN a.message + ', from node ' + id(a)",
                        parameters("message", message));

                return result.single().get(0).asString();
            });

            System.out.println(greeting);
        }
    }

    public static void main(String... args) throws Exception {
        try (HelloWorldExample greeter = new HelloWorldExample("bolt://192.168.33.52:7687", "neo4j", "1234")) {
            greeter.printGreeting("hello, world");
        }
    }
}
